<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Templatemodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'template_id' => 'template.template_id',
        'template_status' => 'template.template_status',
    );
    public static $likeField = array(
      'template_name' => 'template_name',
      'template_support' => 'template_support',
      'template_wish' => 'template_wish',
      'template_time_create_start' => 'template_time_create >=',
      'template_time_create_end' => 'template_time_create <=',

    );
    public static $sortField = array(
      'template_money' => 'template_money',
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getTemplateListSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('template');
        $this->db->where('template_status !=',STATUS_DELETE);
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        if (isset($assist['where_in']) && !empty($assist['where_in'])) {
            foreach ($assist['where_in'] as $k => $v) {
                if(!empty($v) && is_array($v)) $this->db->where_in($k,$v);
                else {
                    $query = $this->db->get();
                    if(isset($assist['nocount'])) return array();
                    return array(array(),0);
                }
            }
        }
        if(!isset($assist['nocount'])) $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        // $this->db->order_by('template_status','desc');
        // $this->db->order_by('template_sort','desc');
        $this->db->order_by('template_id','desc');
        if(isset($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        if(isset($assist['nocount'])) return $list;
        return array($list,$count);
    }


    public function getTemplateSection($assist){
        if(isset($assist['select'])) $this->db->select($assist['select']);
        $this->db->from('template');
        if(isset($assist['where'])) $this->db->where($assist['where']);
        if(isset($assist['like'])) $this->db->like($assist['like']);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getTemplate($template_id){
        $this->db->from('template');
        $this->db->where('template_id',$template_id);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getTemplateById($template_id){
        if(!is_array($template_id)) $template_id = array($template_id);
        if(empty($template_id)) return array();

        $this->db->from('template');
        $this->db->where_in('template_id',$template_id);
        $query = $this->db->get();
        $list = $query->result_array();

        return $list;
    }

    public function getActiveTemplateList() {
      $this->db->from('template');
      $this->db->where('template_status !=',STATUS_DELETE);
      $query = $this->db->get();
      $list = $query->result_array();

      return $list;
    }

/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createTemplate($info){
        $this->db->insert('template',$info);
        $template_id = $this->db->insert_id();
        $this->db->query('insert into {PRE}template_history select null,a.* from {PRE}template as a where template_id='.$template_id);

        return $template_id;
    }

    public function createTemplateBatch($info){
        if (!empty($info)) {
            $this->db->insert_batch('template',$info);
        }
    }

/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
    public function editTemplate($info){
        if(isset($info['template_id'])) {
            $template_id = $info['template_id'];
            unset($info['template_id']);
            $this->db->where('template_id',$template_id);
            $this->db->update('template',$info);
            $this->db->query('insert into {PRE}template_history select null,a.* from {PRE}template as a where template_id='.$template_id);
        }
    }

    public function updateTemplateBatch($update_arr){
        if (!empty($update_arr)) {
            $this->db->update_batch('template',$update_arr,'template_id');
        }
    }

}
